package com.sc.test;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Shen Chao on 2018-03-14-014 上午 9:14.
 */

public class DialogActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_dialog1, R.id.btn_dialog2, R.id.btn_dialog3, R.id.btn_dialog4, R.id.btn_dialog5, R.id.btn_dialog6, R.id.btn_dialog7, R.id.btn_dialog8})
    public void onViewClicked(View view) {
        final String[] items = {"条目1", "条目2", "条目3", "条目4"};
        final int[] yourchoice = {-1};
        switch (view.getId()) {
            case R.id.btn_dialog1:
                AlertDialog.Builder normalDialog =
                        new AlertDialog.Builder(this);
                normalDialog.setIcon(R.drawable.ic_dialog_24dp);
                normalDialog.setTitle("普通弹窗");
                normalDialog.setMessage("点击哪个按钮");
                normalDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, "确定", Toast.LENGTH_SHORT).show();
                    }
                });
                normalDialog.setNeutralButton("第三个", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, "第三个", Toast.LENGTH_SHORT).show();
                    }
                });
                normalDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });
                normalDialog.show();
                break;
            case R.id.btn_dialog2:
                AlertDialog.Builder listDialog = new AlertDialog.Builder(this);
                listDialog.setIcon(R.drawable.ic_dialog_24dp);
                listDialog.setTitle("列表弹窗");
                listDialog.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, items[i], Toast.LENGTH_SHORT).show();
                    }
                });
                listDialog.show();
                break;
            case R.id.btn_dialog3:
                AlertDialog.Builder singChoiceDialog = new AlertDialog.Builder(this);
                singChoiceDialog.setIcon(R.drawable.ic_dialog_24dp);
                singChoiceDialog.setTitle("单选");
                singChoiceDialog.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        yourchoice[0] = i;
                    }
                });
                singChoiceDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (yourchoice[0] != -1) {
                            Toast.makeText(DialogActivity.this, items[yourchoice[0]], Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                singChoiceDialog.show();
                break;
            case R.id.btn_dialog4:
                final boolean choiceSets[] = {true, false, false, true};
                AlertDialog.Builder mutiChoiceDialog = new AlertDialog.Builder(this);
                mutiChoiceDialog.setIcon(R.drawable.ic_dialog_24dp);
                mutiChoiceDialog.setTitle("多选弹窗");
                mutiChoiceDialog.setMultiChoiceItems(items, choiceSets, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int witch, boolean ischecked) {
                        choiceSets[witch] = ischecked;
                    }
                });
                mutiChoiceDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int witch) {
                        String s = "";
                        for (int j = 0; j < choiceSets.length; j++) {
                            if (choiceSets[j]) {
                                s += items[j] + " ";
                            }
                        }
                        Toast.makeText(DialogActivity.this, s, Toast.LENGTH_SHORT).show();
                    }
                });
                mutiChoiceDialog.show();
                break;
            case R.id.btn_dialog5:
                //等待dialog有屏蔽其他控件的交互能力
                final ProgressDialog waitingDialog = new ProgressDialog(this);
                waitingDialog.setIcon(R.drawable.ic_dialog_24dp);
                waitingDialog.setTitle("等待");
                waitingDialog.setMessage("等待中。。。");
                //不确定进度，true为在最大和最小移动，动画效果
                waitingDialog.setIndeterminate(true);
                //可以取消
                waitingDialog.setCancelable(false);
                //点外部取消
                waitingDialog.setCanceledOnTouchOutside(false);
                waitingDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "cancle", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        waitingDialog.dismiss();
                    }
                });
                waitingDialog.show();
                break;
            case R.id.btn_dialog6:
                //setProgress 设置初始进度
                //setProgressStyle 设置样式（水平进度条）
                //setMax 设置进度最大值
                final ProgressDialog progressDialog = new ProgressDialog(this);
                progressDialog.setIcon(R.drawable.ic_dialog_24dp);
                progressDialog.setProgress(0);
                progressDialog.setTitle("我是一个进度条Dialog");
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                progressDialog.setMax(100);
                progressDialog.show();

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i <= 100; i++) {
                            try {
                                Thread.sleep(150);
                                progressDialog.setProgress(i);
                                progressDialog.incrementSecondaryProgressBy(10);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        // 进度达到最大值后，窗口消失
                        progressDialog.cancel();
                    }
                }).start();
                break;
            case R.id.btn_dialog7:
                final EditText editText = new EditText(this);
                AlertDialog.Builder inputDialog = new AlertDialog.Builder(this);
                inputDialog.setIcon(R.drawable.ic_dialog_24dp);
                inputDialog.setTitle("输入弹窗");
                inputDialog.setView(editText);
                inputDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(DialogActivity.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                inputDialog.show();
                break;
            case R.id.btn_dialog8:
                AlertDialog.Builder customDialog = new AlertDialog.Builder(this);
                final View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_custom, null);
                customDialog.setIcon(R.drawable.ic_dialog_24dp);
                customDialog.setTitle("自定义弹窗");
                customDialog.setView(dialogView);
                customDialog.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        EditText customET = dialogView.findViewById(R.id.et_dialog_custom);
                        Toast.makeText(DialogActivity.this, customET.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                });
                customDialog.show();
                break;
        }
    }
}
